#include "StdAfx.h"
#include "EXMemDC.h"

CEXMemDC::CEXMemDC(CDC* pDC, const CRect& rcBounds) : CDC()
{
	CreateCompatibleDC(pDC);
	m_bitmap = new CBitmap;
	m_bitmap->CreateCompatibleBitmap(pDC, rcBounds.Width(), rcBounds.Height());
	m_oldBitmap = SelectObject(m_bitmap);
	m_pDC = pDC;
	m_rcBounds = rcBounds;
	//For some reason the background color is not correct,
	//so we use the button face color.
	DWORD	color = ::GetSysColor( COLOR_BTNFACE );
	CBrush bkg(color);
	FillRect(rcBounds, &bkg);

}

CEXMemDC::~CEXMemDC() 
{
	m_pDC->BitBlt(m_rcBounds.left, m_rcBounds.top, m_rcBounds.Width(), 
                m_rcBounds.Height(), this, m_rcBounds.left, 
                m_rcBounds.top, SRCCOPY);

	SelectObject(m_oldBitmap);
  if (m_bitmap != NULL)   delete m_bitmap;
}

CEXMemDC* CEXMemDC::operator->() {
	return this;
}